﻿using System;
using Microsoft.Xna.Framework;
using System.Timers;
using System.Diagnostics;

namespace ProtoGame.GameLib.Entities
{
    public abstract class Entity
    {
        /*public delegate void CollisionStrategy(Entity player, byte collisionType);
        public delegate bool CheckCollisionStrategy(Entity thisSprite, Entity otherSprite);*/

        Stopwatch lifetimeWatch = new Stopwatch();
        TimeSpan _lifetime = TimeSpan.Zero;

        public Entity()
        {
            Position = Vector2.Zero;
            Alive = true;
            CollisionType = 0;

            /*_lifetime = new Timer();
            _lifetime.Elapsed += new ElapsedEventHandler(lifetime_Elapsed);*/
        }

        /// <summary>
        /// 
        /// </summary>
        public byte CollisionType { get; set; }

        /// <summary>
        /// Posição relativa ao tabuleiro de jogo, e não a tela.
        /// </summary>
        public virtual Vector2 Position { get; set; }

        public override string ToString()
        {
            return String.Format("Position: {0}", Position.ToString());
        }

        /*public CollisionStrategy Collide { private get; set; }

        public CheckCollisionStrategy CheckCollision { private get; set; }*/

        bool _alive;
        public bool Alive
        {
            get { return _alive; }
            set { _alive = value; }
        }

        public abstract Rectangle Bounds { get; }

        /// <summary>
        /// Tempo de vida da entidade (em milisegundos).
        /// </summary>
        public TimeSpan Lifetime
        {
            get { return _lifetime; }
            set
            {
                _lifetime = value;
                if (value > TimeSpan.Zero)
                    lifetimeWatch.Start();
            }
        }

        protected virtual void LifetimeElapsed()
        {
            Alive = false;
            lifetimeWatch.Reset();
            Lifetime = TimeSpan.Zero;
        }

        public virtual bool DoCheckCollision(Entity otherEntity)
        {
            return Bounds.Intersects(otherEntity.Bounds);
        }

        public virtual void Update(GameTime gameTime)
        {
            if (lifetimeWatch.IsRunning && lifetimeWatch.Elapsed >= Lifetime)
                LifetimeElapsed();
        }

        public abstract void DoCollision(byte collisionType);
    }
}
